package handlers

import (
	"ecom-sys/cache"
	"ecom-sys/models"
	"encoding/json"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_type := c.PostForm("good_type")
	good_reason := c.PostForm("good_reason")
	data := models.Good{
		GoodCode:   good_code,
		GoodName:   good_name,
		GoodType:   good_type,
		GoodReason: good_reason,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func GoodOrderAdd(c *gin.Context) {
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	good_order_code := c.PostForm("good_order_code")
	good_order_name := c.PostForm("good_order_name")
	good_order_price, _ := strconv.Atoi(c.PostForm("good_order_price"))
	data := models.GoodOrder{
		GoodOrderCode:  good_order_code,
		GoodOrderName:  good_order_name,
		GoodOrderPrice: good_order_price,
		Gid:            gid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func GoodOrderList(c *gin.Context) {
	/*
		SELECT
		  good_orders.id AS GoodOrderId,
		  good_orders.good_order_code AS GoodOrderCode,
		  good_orders.good_order_name AS GoodOrderName,
		  good_orders.good_order_price AS GoodOrderPrice,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_type AS GoodType,
		  goods.good_reason AS GoodReason
		FROM
		  good_orders
		  LEFT JOIN goods ON good_orders.gid = goods.id
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.GoodOrderList
	redis_key := "good_orders_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select(
				"good_orders.id AS GoodOrderId",
				"good_orders.good_order_code AS GoodOrderCode",
				"good_orders.good_order_name AS GoodOrderName",
				"good_orders.good_order_price AS GoodOrderPrice",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_type AS GoodType",
				"goods.good_reason AS GoodReason",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Offset(offset).Limit(size).Order("GoodOrderId asc").Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
		"msg":   "ok",
	})
	return
}
func GoodOrderDetail(c *gin.Context) {
	/*
		SELECT
		  good_orders.id AS GoodOrderId,
		  good_orders.good_order_code AS GoodOrderCode,
		  good_orders.good_order_name AS GoodOrderName,
		  good_orders.good_order_price AS GoodOrderPrice,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_type AS GoodType,
		  goods.good_reason AS GoodReason
		FROM
		  good_orders
		  LEFT JOIN goods ON good_orders.gid = goods.id
		WHERE good_orders.id=1
	*/
	id := c.Query("id")
	var list models.GoodOrderList
	redis_key := "good_orders_Detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("good_orders").
			Select(
				"good_orders.id AS GoodOrderId",
				"good_orders.good_order_code AS GoodOrderCode",
				"good_orders.good_order_name AS GoodOrderName",
				"good_orders.good_order_price AS GoodOrderPrice",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_type AS GoodType",
				"goods.good_reason AS GoodReason",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "ok",
	})
	return
}
